package com.fanwh.healthycampus2.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.fanwh.healthycampus2.R;
import com.fanwh.healthycampus2.bean.HealthyInformation;
import com.fanwh.healthycampus2.bean.User;

import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobQueryResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SQLQueryListener;

public class PersonalInformationActivity extends AppCompatActivity {

    private ImageButton back;
    private TextView name;
    private TextView studentID;
    private TextView school;
    private TextView tel;
    private TextView title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_information);
        Bmob.initialize(this, "e85fe1093bc0f95a7ef09f6e23de1cae");

        back = findViewById(R.id.ib_navigation_back);
        title = findViewById(R.id.tv_navigation_label);
        name = findViewById(R.id.per_name);
        school = findViewById(R.id.per_school);
        studentID = findViewById(R.id.per_studentID);
        tel = findViewById(R.id.per_tel);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        title.setText("个人信息");


        User user = BmobUser.getCurrentUser(User.class);
        String username = (String) BmobUser.getObjectByKey("username");
        BmobQuery<HealthyInformation> query = new BmobQuery<HealthyInformation>();
        query.addWhereEqualTo("student",user);
        String sql = "SELECT * From HealthyInformation where studentID = '"+username+"'ORDER BY createdAt DESC LIMIT 1";
        query.doSQLQuery(sql, new SQLQueryListener<HealthyInformation>() {
            @Override
            public void done(BmobQueryResult<HealthyInformation> bmobQueryResult, BmobException e) {
                if(e == null){
                    List<HealthyInformation> list = bmobQueryResult.getResults();
                    //Toast.makeText(getActivity(),"查询成功：共"+list.size()+"条数据",Toast.LENGTH_SHORT).show();
                    String str = " ";
                    for(HealthyInformation heathyInformation : list){
                        studentID.setText(heathyInformation.getStudentID());
                        name.setText(heathyInformation.getName());
                        school.setText(heathyInformation.getSchool());
                        tel.setText(heathyInformation.getPhoneNumber());
                        /*
                        if(Double.parseDouble(heathyInformation.getTemperature()) < 37.3){
                            Toast.makeText(getContext(),"体温正常，不用太过担心",Toast.LENGTH_LONG).show();
                        }else {
                            Toast.makeText(getActivity(),"体温异常，建议您去医院瞧瞧",Toast.LENGTH_LONG).show();
                        }

                         */
                    }

                }else {
                    System.out.println(e.getErrorCode());
                }
            }
        });




    }
}
